package com.ato.CommonClass.compare;

/**
 * @author wangjing
 * @create 2021--11--01--20:24
 */
public class Goods implements Comparable {

    private String name;
    private Double price;

    @Override
    public int compareTo(Object o) {
        if (o instanceof Goods) {
            Goods goods = (Goods) o;
            if (this.price > goods.price) {
                return 1;
            } else if (this.price < goods.price) {
                return -1;
//        以上可以简写成: return Double.compare(this.price,goods.price)
            } else {
                return 0; //  如果价格相等时，这里在排序名称则可以，return this.name.compare(this.name,goods.name)
            }
        }
        throw new RuntimeException("数据类型不正确，无法比较");
    }
    // 方式2：包装类是有comparable的
//        Goods goods = (Goods) o;
//        return Double.compare(this.price,goods.price);


    public Goods() {
    }

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public Goods(String name, Double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }


}
